<script>
	$(document).ready(function() {
		
		$('.star').hover(
			function() {
				if(getCookie($(this).parent().attr('id')) != 'done') {
					var elem = $(this);
					var params = ($(this).attr('rel')).split(',');
					updateRating(elem, params[0]);
				}
			},
			function() {
				if(getCookie($(this).parent().attr('id')) != 'done') {
					var elem = $(this);
					var rate = elem.parent().attr('rate');
					updateRating(elem, rate);
				}
			}
		);
		$('.star').click( function() {
			if(getCookie($(this).parent().attr('id')) != 'done') {
				var params = ($(this).attr('rel')).split(',');
				var elem = $(this);
				$.ajax({
					type: "POST",
					url:"<?php echo 'search/rating';?>",
					data:{
						id: params[1],
						mark: params[0]
					},
					success:function(data) {
						var rate = data.split('!');
						rate = rate[1];
						alert(rate);
						elem.parent().attr('rate', rate);
						updateRating(elem, rate);
						var elemp = elem.parent().find('span');
						elemp.html(parseInt(elemp.attr('rel')) + 1);
						elemp.attr('rel', parseInt(elemp.attr('rel')) + 1);
						setCookie(elem.parent().attr('id'), 'done', 200);
						
					},
					error: function() {}
				});
			}
		});
		function setCookie(cookieName,cookieValue,nDays) {
			var today = new Date();
			var expire = new Date();
			if (nDays==null || nDays==0) nDays=1;
			expire.setTime(today.getTime() + 3600000*24*nDays);
			document.cookie = cookieName+"="+escape(cookieValue)
						 + ";expires="+expire.toGMTString();
		}
		 
		function getCookie(name) {
			var dc = document.cookie;
			var prefix = name + "=";
			var begin = dc.indexOf("; " + prefix);
			if (begin == -1) {
				begin = dc.indexOf(prefix);
				if (begin != 0) return null;
			} else {
				begin += 2;
			}
			var end = document.cookie.indexOf(";", begin);
			if (end == -1) {
				end = dc.length;
			}
			return unescape(dc.substring(begin + prefix.length, end));
		}
		function updateRating(elem, rate) {
			$('#'+elem.parent().attr('id')+' div').each(function(key, value) {
				if(key < rate) {
					$(value).removeClass('s_grey').addClass('s_yellow');
				} else {
					$(value).removeClass('s_yellow').addClass('s_grey');
				}
			});
		}
	});
</script>
<div id="content">
        <?php echo $this->load->view('_elements/navigate'); ?>
        <div id="content_bottom">
            <div class="content_bottom_left">
                <div class="content_bottom_left_first blockstyle">
                	<div id="section_list">
						<div id="section_list_title"><?php echo T_('List of Sections'); ?></div>
					          	
						<ol id="content_bottom_left_first_list">
							<?php if(!empty($sections) && count($sections) > 0) : ?>
								<?php foreach($sections as $section) : ?>
									<li><?php echo anchor('section/'.$section->section_name, T_($section->section_name)); ?></li>
								<?php endforeach; ?>
							<?php endif; ?>		
						</ol>		
					</div>		
                </div>
                <div class="content_bottom_left_second blockstyle">
                Ad Area;<br />
				Maybe Ladders,<br />
				CareerBuilder,<br />
				etc
                </div>
            </div>
            <div class="content_bottom_center">
				<p class="title">Home Page Title</p>
				<div id="search_blok">
					<?php echo form_open('/search', 'id="search_form" name="search_form" autocomplete="off"')?> 
						<div class="content_navigate_search">
							<input class="search_input" type="text" name="query" value="<?php echo $last_query;?>"></input>
						</div>
						<input class="ok_search" name="search" type="submit" value="<?php echo T_('Search in Books') ?>"></input>
						<input class="search_input" name="search_name" type="checkbox" style="clear:both;" <?php echo ($search_name) ? 'checked' :''; ?>></input><span class="search_label"><?php echo T_('Search for book name'); ?></span><br>
						<input class="search_input" name="search_lang" type="checkbox" style="clear:both;" <?php echo ($lang_only) ? 'checked' :''; ?>></input><span class="search_label"><?php echo T_('Search only russian'); ?></span>
					<?php echo form_close() ?>
				</div>
	            <?php foreach ($book_list as $i => $row): ?>
					<div class="one_book_blok blockbg">
						<img src="<?php echo ((file_exists('/bookImg/'.$row->lit_guid.'.png')) ? '/bookImg/'.$row->lit_guid.'.png' : '/bookImg/not_found.png'); ?>" style="min-width: 125px; padding:16px 16px 13px 16px; float: left;"/>
						<div class="book_info">
						<div class="book_name book" style=""><?php echo $row->lit_name?></div>
						<div rate="<?php echo round($row->lit_mark); ?>" id="book_rate<?php echo $row->lit_id;?>" style="position:absolute; top: 5px; right: 10px;">
							<?php for($i = 0; $i < 5; $i++) : ?>
								<div rel="<?php echo($i+1).','.$row->lit_id; ?>" class="star <?php echo 's_'.( ($i < round($row->lit_mark)) ? 'yellow' : 'grey' )?>"style="float: left"></div>
							<?php endfor; ?>
							<div id="voters<?php echo $row->lit_id; ?>" class="voters" style="color:#000;">
								<img src="/img/style/rating_count.png" style="margin:3px 0 0 0; display:block;float:right;"/>
								<span rel="<?php echo $row->lit_voters;?>" style="float:right; margin: 5px 4px 0 0;"><?php echo $row->lit_voters;?></span>
							</div>
						</div>
						<div class="book_author book"><?php echo $row->lit_author?></div>
						<div class="book_about book" style="color: #000000; margin: 10px 15px;"><?php echo substr($row->lit_description, 0, 500).'...';?></div>
						</div>
						<div id="actions"><a href="/Download.php?name=<?php echo 'bookFile/'.$row->lit_guid;?>.pdf">Download</a><a href="/rus-html-5.pdf" >Read online</a><a href="/book_info" >Read more</a></div>
					</div>		
		        <?php endforeach; ?>
            </div>
        </div>
</div>